import { session } from 'js/util'
import { apiApplyRoleInfo } from 'js/api'

export default {
  state: {
    enterTime: '',
    enterType: '',
    enterTypeName: '',
    enterInfo: session.get('enterInfo') || {}, // 入驻商户信息
    status: '', // 用户状态 “not_apply”未申请 “not_pay”未支付 “review”审核中 “success”通过 “failed”不通过 “overdue”过期
    cause: '' // 审核结果
  },
  getters: {
    enterTime: state => state.enterTime,
    enterType: state => state.enterType,
    enterTypeName: state => state.enterTypeName,
    status: state => state.status,
    cause: status => status.cause,
    enterInfo: state => state.enterInfo
  },
  actions: {
    fetchApplyRoleInfo({ state, commit }, payload) {
      apiApplyRoleInfo(payload.token).then(data => {
        state.status = data.status
        state.cause = data.cause
        state.enterTime = (data.date && data.date.split(' ')[0]) || ''
        state.enterInfo = data.roleApply || {}
        session.set('enterInfo', state.enterInfo)
        state.enterType = data.type
        state.enterTypeName = state.enterType === 'purchaser' ? '采购商' : state.enterType === 'supplier' ? '供应商' : '普通会员'
        payload.callback && payload.callback(data || {})
      })
    }
  },
  mutations: {
    updataApplyType(state, payload) {
      state.enterType = payload
    }
  }
}
